import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {ConfigService} from "./config.service";
import {ProductConfig} from "../../shared/model/product-config";
@Injectable({
    providedIn: 'root'
})
export class CustomService {

    public product: any;

    constructor(private http: HttpClient, private configService: ConfigService) {
    }

    load() {
/*        const promise_arr = [];
        promise_arr.push(new Promise((resolve) => {
            this.http.get(this.configService.api.url + "/config/project")
                .subscribe((data: any) => {
                    ProjectConfig.getInstance().init();
                    ProjectConfig.getInstance().initMeta();
                    ProjectConfig.getInstance().fromJSON(data);
                    ProjectConfig.getInstance().relate();
                    // ProjectConfig.getInstance().initFilter();
                    this.project = ProjectConfig.getInstance();
                    resolve();
                });
        }));
        promise_arr.push(new Promise((resolve) => {
            this.http.get(this.configService.api.url + "/config/asset")
                .subscribe((data: any) => {
                    AssetConfig.getInstance().init();
                    AssetConfig.getInstance().initMeta();
                    AssetConfig.getInstance().fromJSON(data);
                    AssetConfig.getInstance().relate();
                    this.asset = AssetConfig.getInstance();
                    //console.log(AssetConfig.getInstance().toJSON());
                    resolve();
                });
        }));
        promise_arr.push(new Promise((resolve) => {
            this.http.get(this.configService.api.url + "/config/monitor")
                .subscribe((data: any) => {
                    MonitorConfig.getInstance().init();
                    MonitorConfig.getInstance().initMeta();
                    MonitorConfig.getInstance().fromJSON(data);
                    MonitorConfig.getInstance().relate();
                    this.monitor = MonitorConfig.getInstance();
                    // console.log(MonitorConfig.getInstance().toJSON());
                    resolve();
                });
        }));
        promise_arr.push(new Promise((resolve) => {
            this.http.get(this.configService.api.url + "/config/agriculture")
                .subscribe((data: any) => {
                    AgricultureConfig.getInstance().init();
                    AgricultureConfig.getInstance().initMeta();
                    AgricultureConfig.getInstance().fromJSON(data);
                    AgricultureConfig.getInstance().relate();
                    this.agriculture = AgricultureConfig.getInstance();
                    // console.log(AgricultureConfig.getInstance().toJSON());
                    resolve();
                });
        }));
        return Promise.all(promise_arr);*/

        const promise_arr = [];
        promise_arr.push(new Promise((resolve) => {
            this.http.get(this.configService.api.url + "/config/product")
                .subscribe((data: any) => {
                    ProductConfig.getInstance().initMeta();
                    ProductConfig.getInstance().fromJSON(data);
                    ProductConfig.getInstance().relate();
                    this.product = ProductConfig.getInstance();
                    resolve(this.product);
                });
        }));
        return Promise.all(promise_arr);
    }

}
